<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix='sec'
	uri='http://www.springframework.org/security/tags'%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
	<script type="text/javascript" src="scripts/boot.js"></script>
</head>
<body style="border: 0px; margin: 0px; padding: 0px;width: 100%;height: 100%">
	<div class="mini-toolbar" style="border-bottom:0;padding:0px;">
	    <table style="width:auto;">
	        <tr>
	            <td>
	            <sec:authorize url='/medicine_sort_add.do'>
					<a class="mini-button" iconCls="icon-add" onclick="onAddNode('添加子类别')">增加</a>
				</sec:authorize>
				<sec:authorize url='/medicine_sort_edit.do'>
					<a class="mini-button" iconCls="icon-edit" onclick="onEditNode('修改类别')">修改</a>
				</sec:authorize>
				<sec:authorize url='/medicine_sort_del.do'>
					<a class="mini-button" iconCls="icon-remove" onclick="onRemoveNode()">删除</a>
				</sec:authorize>
	            </td>
	        </tr>
	    </table>           
	</div>
    <fieldset style="width:auto;height:auto;solid 1px #aaa;position:relative;top: 15;">
		<ul id="tree1" class="mini-tree" url="medicine_sort_view.do" style="width:auto;height:auto;padding:5px;" 
	   	showTreeIcon="true" textField="name" idField="id" parentField="pid" resultAsTree="false" expandOnLoad="true" showTreeLines="true"> 
		</ul>
	</fieldset>
<script type="text/javascript">
	
	function onAddNode(title) {
	    var tree = mini.get("tree1");
	    var node = tree.getSelectedNode();
	    var newNode = {};
	    if(node){
			mini.open({
    	   		url: "medicine_sort_add.do",
    	   		allowResize:false,
               	title: title, width: 300, height: 200,
               	onload: function () {
                   var iframe = this.getIFrameEl();
                   iframe.contentWindow.SetData(node);
           		},
	    	   	ondestroy: function (action) {
	    	   		if(action == 'succ'){
	    	   			mini.showMessageBox({
		                    title: "提示",
		                    showModal:false,
		                    iconCls: "mini-messagebox-info",
		                    message: "保存成功！",
		                    timeout: 3000,
		                    x: 'right',
		                    y: 'bottom'
		                });
		    	   		tree.addNode(newNode, "add", node);
		    	   		tree.load('medicine_sort_view.do');
	    	   		}
                }
         	});
	    }else{
	    	mini.alert("请选择一个类别！");
	    }
	}
		
	function onEditNode(title) {
	    var tree = mini.get("tree1");
	    var node = tree.getSelectedNode();
	    if(node){
	    	if("" == node.pid){
		    	mini.alert("药品类别为根节点，不能编辑！");
	    	}else{
	    		mini.open({
	    	   		url: "medicine_sort_edit.do",
	    	   		allowResize:false,
	               	title: title, width: 300, height: 200,
	               	onload: function () {
	                   var iframe = this.getIFrameEl();
	                   iframe.contentWindow.SetData(node);
           			},
		    	   	ondestroy: function (action) {
		    	   		if(action == 'succ'){
		    	   			mini.showMessageBox({
			                    title: "提示",
			                    showModal:false,
			                    iconCls: "mini-messagebox-info",
			                    message: "修改成功！",
			                    timeout: 3000,
			                    x: 'right',
			                    y: 'bottom'
			                });
			    	   		tree.load('medicine_sort_view.do');
		    	   		}
	                }
	         	});
	    	}
	    }else{
	    	mini.alert("请选择一个类别！");
	    }
	}
	
	function onRemoveNode(e) {
	    var tree = mini.get("tree1");
	    var node = tree.getSelectedNode();
	    if (node) {
	    	if("" == node.pid){
	        	mini.alert("药品类别为根节点，不能删除！");	
	    	}else{
	    		mini.confirm("确定删除记录？", "确定？",
	 	 	           function (action) {
	 	 	               if (action == "ok") {
	 	 	            	   $.post('medicine_sort_judge.do', {id: node.id}, function(data){
	 	 	            		   	if(data.result == 'error'){
	 	 	            		   		mini.alert("数据错误，类别id不能为空");
	 	 	            		   	}else if(data.result == 'true'){
	 	 		   	    				mini.alert("当前类别下有子类别，不能删除，请先删除子类别！");
	 	 		   	    			}else{
 	 		   	    					$.post('medicine_sort_use.do', {id: node.id}, function(data){
 	 		   	    						if(data.result == 'error'){
 	 		   	    							mini.alert("数据错误，类别id不能为空");
 	 		   	    						} else if(data.result == 'true'){
 	 		   	    							mini.alert("当前类别下已添加药品，无法删除！");
 	 		   	    						}else{
	 	 		   	    						$.post('medicine_sort_del.do', {id: node.id}, function(data){
			 	 		   	    					if(data.success){
				 	 		   	    					mini.showMessageBox({
				 	 					                    title: "提示",
				 	 					                    showModal:false,
				 	 					                    iconCls: "mini-messagebox-info",
				 	 					                    message: "删除成功！",
				 	 					                    timeout: 3000,
				 	 					                    x: 'right',
				 	 					                    y: 'bottom',
				 	 					                });
				 	 		   	    					tree.load('medicine_sort_view.do');
			 	 		   	    					}else{
			 	 		   	    						mini.alert("id不能为空，删除失败！");
			 	 		   	    					}
			 	 		   	    				}, "json");
 	 		   	    						}
 	 		   	    					}, "json");
	 	 		   	    			}
	 	 	   	    			}, "json");
	 	 	               }
	 	 	           }
           		);
	    	}
	    }else{
	    	mini.alert("请选择一个类别！");
	    }
	}

</script>
</body>
</html>